package cn.com.yang.task;

import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * SpringBoot定时任务
 * TODO
 * @author yangyijin
 * @since 2023年5月23日-下午6:03:27
 */
@Component
@EnableScheduling
public class TaskDemo implements SchedulingConfigurer{

	private AtomicInteger n =new AtomicInteger(0);
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		for(int i=0;i<100;i++ ) {
			taskRegistrar.addTriggerTask(()->doTask(),triggerContext ->{
				//秒 分 时 日 月 周 年
				String cron = "* 0/30 * * * ?";
				
				return new CronTrigger(cron).nextExecutionTime(triggerContext);
			});
		}
	}

	private Object doTask() {
		 n.addAndGet(1);
		System.out.println("@EnableScheduling执行了"+n.get()+"次");
		return null;
	}
	
	
}
